%% This m-file is to display the reflectivity data
close all
clear all
clc
RefTxtFile={
   'sph01_112_ref.txt';   %  #1 sph01.dat SPM+ 100 mM KCl; pH 3;
    'sph01_141_ref.txt';   %  #2 sph01.dat SPM+ 100 mM KCl; pH 4;
    'sph01_177_ref.txt';   %  #3 sph01.dat SPM/Fe+ 100 mM KCl; pH 3;
    'sph01_251_ref.txt';   %  #4 sph01.dat SPM/Fe+ 100 mM KCl; pH 7.08;
    'sph01_321_ref.txt';   %  #5 sph01.dat SPM/Fe+ 100 mM KCl; pH 4;
    'sph02_27_ref.txt';   %   #6 sph02.dat SPM/Fe+ 100 mM KCl; pH 2;
    'sph02_92_ref.txt';   %   #7 sph02.dat SPM/Fe+ 100 mM KCl; pH 5;
    'sph02_152_ref.txt';   %   #8 sph02.dat SPM/Fe+ 100 mM KCl; pH 5;
    'sph02_215_ref.txt';   %   #9 sph02.dat SPM/Fe+ 100 mM KCl; pH 3;
    'sph02_274_ref.txt';   %   #10 sph02.dat SPM/Fe+ 100 mM KCl; pH neutral 
    'sph02_333_ref.txt';   %   #11 sph02.dat SPM/Fe+ 100 mM KCl; pH neutral 
    'sph02_341_ref.txt';   %   #12 sph02.dat SPM/Fe+ 100 mM KCl; pH neutral 
    }

OptimParamSave={
    'sph01_112_ref_optim.txt';   %  #1  sph01.dat SPM    + 100 mM KCl; pH 3
    'sph01_141_ref_optim.txt';   %  #2  sph01.dat SPM    + 100 mM KCl; pH 4
    'sph01_177_ref_optim.txt';   %  #3  sph01.dat SPM/Fe + 100 mM KCl; pH 3
    'sph01_251_ref_optim.txt';   %  #4  sph01.dat SPM/Fe + 100 mM KCl; pH 7.08
    'sph01_321_ref_optim.txt';   %  #5  sph01.dat SPM/Fe + 100 mM KCl; pH 4
    'sph01_27_ref_optim.txt';    %  #6  sph02.dat SPM/Fe + 100 mM KCl; pH 2
    'sph01_92_ref_optim.txt';    %  #7  sph02.dat SPM/Fe + 100 mM KCl; pH 2
    'sph02_152_ref_par.mat';   %   #8  sph02.dat SPM/Fe + 100 mM KCl; pH 5
    'sph02_215_ref_par.mat';   %   #9  sph02.dat SPM/Fe + 100 mM KCl; pH 3
    'sph02_274_ref_par.mat';   %   #10 *  sph02.dat SPM/Fe + 100 mM KCl; pH neutral
    'sph02_333_ref_par.mat';   %   #11 * sph02.dat SPM/Fe + 100 mM KCl; pH neutral
    'sph02_341_ref_par.mat';   %   #12  sph02.dat SPM/Fe + 100 mM KCl; pH neutra
    }

COLOR={'k','b','r',[0 0.4 0],'m',[0.4 0.4 0],[0 0.4 0.6],'b','r'}
Marker={'o','s','v','^','<','>','d'};

C_length=10;
C=zeros(1,C_length);
C(1)=150; % 100 slices
C(6)=0;
C(5)=6.8288; % 2*pi/lambda
%%

PlotRange=[1,3,4,5]

close all
figure
subplot(1,2,1)
hold on
counter=1;
C(2)=0;
for k=PlotRange
    qfit=(0:0.0001:0.5)';
    P=dlmread(OptimParamSave{k});
    dq=P(1);
    I0=P(3);
  
    rfresnel_fit=FresnelReflection(qfit-dq,0.0217,0,1.54);
    rof_fit=fitref_ED_3box(P,C,qfit)./rfresnel_fit/I0;

    data=dlmread(RefTxtFile{k});
    q=data(:,1);
    ref=data(:,2);
    sref=data(:,3);
   
    rfresnel=FresnelReflection(q-dq,0.0217,0,1.54);
    rof=ref./rfresnel/I0;

    plot(qfit,rof_fit,'-','LineWidth',3,'Color',COLOR{counter});
    plot(q,rof,'Marker',Marker{counter},'MarkerFaceColor','w',...
        'MarkerSize',12,'Color',COLOR{counter},...
        'LineWidth',3,'linestyle','none');
    counter=counter+1;
end
hold off
set(gca,'yscale','log');
set(gca,'LineWidth',3);
set(gca,'xlim',[0 0.51]);
%set(gca,'xtick',[0:0.05:0.25]);
set(gca,'xtick',[0:0.1:0.5]);
set(gca,'ylim',[0.5e-2,1e1]);
% set(gca,'xtick',[0:0.1:0.5]);
% set(gca,'ylim',[0.5e-1,1e1]);
set(gca,'box','on');
set(gca,'FontSize',30);


subplot(1,2,2);
hold on
counter=1
for k=PlotRange
    z=(-500:1:500)';
    C(2)=1;
    P=dlmread(OptimParamSave{k});
    rho=fitref_ED_3box(P,C,z);
    I=length(rho);
    for k=length(rho):-1:1
        if rho(k)>0.1;
            I=k;break;
        end
    end
%     drho=diff(rho);
%     [~,I]=max(-drho);
    znew=z-z(I);
    plot(znew,rho,'-','LineWidth',3,'Color',COLOR{counter});
    counter=counter+1;
end
hold off

set(gca,'LineWidth',3);
set(gca,'xlim',[-60,30]);
set(gca,'xtick',[-50:20:30]);
set(gca,'ylim',[-0.1,0.71]);
set(gca,'ytick',[0:0.1:0.7]);
% set(gca,'ylim',[-0.1,1.1]);
% set(gca,'ytick',[0:0.5:1.0]);
set(gca,'box','on');
set(gca,'FontSize',30);

